package com.Listener;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

public class DasourceListener implements ServletContextListener {


    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext=sce.getServletContext();
        try {
          Context context = new InitialContext();
          DataSource source =(DataSource)context.lookup("java:comp/env/jdbc/news");
          servletContext.setAttribute("source",source);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("web容器销毁");
    }
}
